package com.zzvcom.service.admin;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzvcom.persist.model.admin.ProductSize;
import com.zzvcom.template.hibernate.DefaultManager;

@Service
@Transactional
public class ProductSizeManager extends DefaultManager<ProductSize, String>
{

    @Transactional
    public void syncSize(String size)
    {
        DetachedCriteria dc = DetachedCriteria.forClass(ProductSize.class);
        dc.add(Restrictions.eq("size", size));
        if (this.countByCriteria(dc) == 0)
        {
            ProductSize pz = new ProductSize();
            pz.setSize(size);
            this.save(pz);
        }
    }

    @Override
    public void save(ProductSize entity)
    {
        
        if(StringUtils.isNotBlank(entity.getId()))
        {
            // update literal value of size property
            ProductSize old=this.get(entity.getId());
            this.updateByHQL(" update Product set size=? where size=?", entity.getSize(),old.getSize());    
            this.entityDao.evict(old);
        }
        super.save(entity);
    }
    
  

}
